package com.nhic.blog.web.admin;

import com.nhic.blog.po.Blog;
import com.nhic.blog.po.Comment;
import com.nhic.blog.po.User;
import com.nhic.blog.service.BlogService;
import com.nhic.blog.service.CommentService;
import com.nhic.blog.service.TagsService;
import com.nhic.blog.service.TypeService;
import com.nhic.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class BlogsController {

    private static final String INPUT = "admin/blogs-input";
    private static final String LIST = "admin/blogs";
    private static final String REDIRECT_LIST = "redirect:/admin/blogs";
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagsService tagsService;
    @Autowired
    private CommentService commentService;
    @GetMapping("/blogs")
    public String blogs(@PageableDefault(size = 2, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                        BlogQuery blog, Model model) {
        model.addAttribute("types", typeService.listType());
        model.addAttribute("page", blogService.listBlog(pageable, blog));
        return LIST;
    }
    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 2,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                         BlogQuery blog, Model model) {
        model.addAttribute("page", blogService.listBlog(pageable, blog));
        return "admin/blogs :: blogList";
    }
    @GetMapping("/blogs/input")
    public String input(Model model) {
        setTypeAndTag(model);
        model.addAttribute("blog", new Blog());
        return INPUT;
    }

    private void setTypeAndTag(Model model) {
        model.addAttribute("tags", tagsService.listTag());
        model.addAttribute("types", typeService.listType());
    }
    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id, Model model) {
        setTypeAndTag(model);
        Blog blog = blogService.getBlog(id);
        blog.init();
        model.addAttribute("blog",blog);
        return INPUT;
    }
    //新增和修改 组合在一起
    @PostMapping("/blogs")
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session) {
        //获取id，为了下面的三元表达式，id为空，表示从新增入口进来的。不为空表示从修改入口进来
        Long id = blog.getId();
        //为blog设置User对象
        blog.setUser((User)session.getAttribute("user"));
        //根据隐藏控件上传的type的id设置blog的type对象
        blog.setType(typeService.getType(blog.getType().getId()));
        //隐藏控件上传的tagIds数组要先转化为集合，因此有convertUtil工具类
        blog.setTags(tagsService.listTag(blog.getTagIds()));
        Blog t = blogService.saveBlog(blog);
        if (t == null) {
            attributes.addFlashAttribute("message", id==null?"新增失败":"修改失败");
        } else {
            attributes.addFlashAttribute("message", id==null?"新增成功":"修改成功");
        }
        return REDIRECT_LIST;
    }
    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes) {
        //如果有评论
        if (commentService.listCommentByBlogId(id).size()>0) {
            //删除之前，先设置comment表的外键 blog id为null
            int i = commentService.updateCommentsByBlogId(id);
            if (i > 0) {
                //删除blog
                blogService.deleteBlog(id);
            }
            //删除comment表外键为空的数据
            commentService.deleteComments();
        } else {
            //删除blog
            blogService.deleteBlog(id);
        }
        attributes.addFlashAttribute("message", "删除成功！！");
        return REDIRECT_LIST;
    }
}
